﻿

namespace Behaviours.Entities
{
  public class Object
  {
    internal int hashCode;

    public Object() => this.hashCode = this.GetType().GetHashCode();

    public virtual void Awake()
    {
    }

    public virtual void OnDestroy()
    {
    }

    public static void Destroy(Object obj, bool reuse = true)
    {
      switch (obj)
      {
        case Behaviour component:
          component.entity.components.Remove(component);
          if (component is IUpdate update)
            component.entity.updates.Remove(update);
          if (reuse)
            component.entity.manager.Push((Object) component);
          obj.OnDestroy();
          break;
        case Entity entity:
          entity.manager.RemoveEntity(entity);
          entity.OnDestroy();
          while (entity.components.Count > 0)
          {
            Behaviour component = entity.components[0];
            entity.components.RemoveAt(0);
            component.OnDestroy();
            if (reuse)
              entity.manager.Push((Object) component);
          }
          entity.updates.Clear();
          if (reuse)
            entity.manager.Push((Object) entity);
          break;
      }
    }

    public virtual void Serialize()
    {
      
    }
    public virtual void Deserialize()
    {
      
    }
  }
}
